/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.app;

import han.bifang.gwt.client.app.content.AppDetailContent;
import han.bifang.gwt.client.app.content.ApplicationRootContent;
import han.bifang.gwt.client.app.content.CreateOrModifyOrRemoveAppContent;
import han.bifang.gwt.client.app.tree.AppTreeItem;
import han.bifang.gwt.client.app.tree.ApplicationTree;
import han.bifang.gwt.client.common.ContentChangeListener;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.TreeNodeChangeListener;
import han.bifang.gwt.client.common.TreeNodeContentListener;
import han.bifang.gwt.client.common.entity.Application;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * 
 * @Author Kent
 */
public class ApplicationTab extends Composite implements
		TreeNodeContentListener, ContentChangeListener {

	private TreeItem selectedItem = null;

	private DecoratorPanel panel = null;

	private AppMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private TreeNodeChangeListener listener = null;

	private GwtContext context = null;
	
	private HorizontalPanel hp;
	
	
	
	
	public void setTreeNodeChangeListener(TreeNodeChangeListener l) {

		this.listener = l;
	}

	public ApplicationTab(BifangGWTServiceAsync service, GwtContext context) {
		this.service = service;
		this.context = context;
		
		cons = GWT.create(AppMessages.class);

		hp = new HorizontalPanel();
		hp.setWidth("100%");

		
		// hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		initWidget(hp);

	}

	public void onTreeNodeSelected(TreeItem item) {
		this.selectedItem = item;
		AppTreeItem i = (AppTreeItem)item;
		if(i.getSubType().equals(AppConstants.APP_ROOT)){
			ApplicationRootContent arc = new ApplicationRootContent(cons,service,context);
			arc.setContentChangeListener(this);
			this.panel.setWidget(arc);
			return;
			
		}else if(i.getSubType().equals(EntityConstants.TREE_APP_APP)){
			AppDetailContent adc = new AppDetailContent(cons,service,i.getKey(),context);
			adc.setContentChangeListener(this);
			this.panel.setWidget(adc);
			return;
		}

	}
	
	public void onLoad() {
		DecoratorPanel treePanel = new DecoratorPanel();
		treePanel.setWidth("100%");

		ApplicationTree tree = new ApplicationTree(cons, service, context);
		tree.setTreeNodeContentListener(this);
		this.setTreeNodeChangeListener(tree);

		treePanel.add(tree);
		hp.add(treePanel);

		panel = new DecoratorPanel();
		panel.setWidth("100%");
		hp.add(panel);
		hp.setCellWidth(treePanel, "30%");
		hp.setCellWidth(panel, "70%");
		
		ApplicationRootContent drc = new ApplicationRootContent(cons, service,context);
		//drc.setWidth("100%");
		drc.setContentChangeListener(this);
		panel.setWidget(drc);

	}


	public void onContentChanged(String source, String dest, HashMap props) {
		if(source.equals(AppContentConstants.APP_ROOT) && dest.equals(AppContentConstants.CREATE_APP)){
			HashMap p = new HashMap();
			p.put("action", CreateOrModifyOrRemoveAppContent.CREATE);
			CreateOrModifyOrRemoveAppContent ac = new CreateOrModifyOrRemoveAppContent(cons,service,context,
					p);
			ac.setContentChangeListener(this);
			this.panel.setWidget(ac);
			return;
		}
		if(source.equals(AppContentConstants.CREATE_APP)				
				&& dest.equals(AppContentConstants.APP_ROOT)){
			Application app = (Application)props.get("app");
			AppTreeItem node = new AppTreeItem();
			node.setHTML(app.getName());
			node.setId(app.getUnitId());
			node.setKey(app.getId());
			node.setType(EntityConstants.TREE_APP);
			node.setSubType(EntityConstants.TREE_APP_APP);
			
			this.listener.onAddNode(node);
			ApplicationRootContent arc = new ApplicationRootContent(cons,service,context);
			arc.setContentChangeListener(this);
			this.panel.setWidget(arc);
			return;
		}
		if(source.equals(AppContentConstants.APP_DETAIL) && dest.equals(AppContentConstants.MODIFY_APP)){
			
			props.put("action", CreateOrModifyOrRemoveAppContent.MODIFY);
			CreateOrModifyOrRemoveAppContent ac = new CreateOrModifyOrRemoveAppContent(cons,service,context,
					props);
			ac.setContentChangeListener(this);
			this.panel.setWidget(ac);
			return;
		}
		if(source.equals(AppContentConstants.MODIFY_APP)				
				&& dest.equals(AppContentConstants.APP_DETAIL)){
			Application app = (Application)props.get("app");
			selectedItem.setHTML(app.getName());
			AppDetailContent arc = new AppDetailContent(cons,service,app.getId(),context);
			arc.setContentChangeListener(this);
			this.panel.setWidget(arc);
			return;
		}
		if(source.equals(AppContentConstants.APP_DETAIL)				
				&& dest.equals(AppContentConstants.REMOVE_APP)){
			
			props.put("action", CreateOrModifyOrRemoveAppContent.REMOVE);
			CreateOrModifyOrRemoveAppContent ac = new CreateOrModifyOrRemoveAppContent(cons,service,context,
					props);
			ac.setContentChangeListener(this);
			this.panel.setWidget(ac);
			return;
		}
		if(source.equals(AppContentConstants.REMOVE_APP)				
				&& dest.equals(AppContentConstants.APP_DETAIL)){
			AppTreeItem i = (AppTreeItem)selectedItem;
			
			AppDetailContent arc = new AppDetailContent(cons,service,i.getKey(),context);
			arc.setContentChangeListener(this);
			this.panel.setWidget(arc);
			return;
		}if(source.equals(AppContentConstants.REMOVE_APP)				
				&& dest.equals(AppContentConstants.APP_ROOT)){
			listener.onRemoveNode(selectedItem);
			
			ApplicationRootContent arc = new ApplicationRootContent(cons,service,context);
			arc.setContentChangeListener(this);
			this.panel.setWidget(arc);
			return;
		}

	}

}
